<?php
/**
 * 使用说明  默认控制器  资费说明
 */
class Controller_baodan extends Controller_Abstract
{

    function _before_execute(){
        
    }
    function actionapplybd(){
        if($this->_context->isGET()){
            if(!isset($_COOKIE['user_id'])){
              header('location: /baodan/login');die;
            }else{
                $user = Acctuser::find('id = ?',$_COOKIE['user_id'])->getOne();
                if(!$user->id()){
                    header('location: /baodan/login');die;
                }
            }
            $dec = Helper_Common::getPublicSet('applydec', 1);
            $dec = empty($dec)?'后台自定义':$dec;
            $this->_view['applydec'] = $dec;
        }else{
            $user = Acctuser::find('id = ?',$_COOKIE['user_id'])->getOne();
            $user->bd_status = -1;
            $user->is_bd = 1;
            $user->save();
            exit(json_encode(array('code'=>0,'msg'=>'报单中心申请成功')));
            
        }
 
        
    }
    function actionhome(){
        $dec = Helper_Common::getPublicSet('aboutus', 1);
        $dec = empty($dec)?'后台自定义':$dec;
        $this->_view['dec'] = $dec;
    }
    function actioneditinfo(){
      if(!isset($_COOKIE['user_id'])){
          header('location: /baodan/login');die;
        }else{
            $user = Acctuser::find('id = ?',$_COOKIE['user_id'])->getOne();
            if(!$user->id()){
                header('location: /baodan/login');die;
            }
        }
        
        if($this->_context->isPOST()){
             
            $upfiles = (isset($_FILES['upfile']))?$_FILES['upfile']:'';
         //print_r($upfiles);die;
            if(isset($upfiles['name']) && $upfiles['name'] ){
                 
                $allowExt = array('jpg','png','jpeg','pjpeg');
                $localName = $upfiles['name'];
                $fileInfo = pathinfo($localName);
                $extension = $fileInfo['extension'];
                if(!in_array(strtolower($extension) , $allowExt)){
                    exit("文件后缀错误");
                }
                $path = Q::ini ( 'app_config/ROOT_DIR' ) . '/public/point/img/header';
                 
                if (! is_dir ( $path )) {
                    @mkdir ( $path, 0700 );
                }
        
                $filename =  $user->id.'_'.time().'.jpg';
                move_uploaded_file($upfiles["tmp_name"],$path.'/'.$filename);
                $filepath = $path.'/'.$filename;
        
                $host = Helper_Common::getRequestHost ();
                if($user->avatar){
                    $oldpath = str_replace($host, '', $user->avatar);
                }
                 
                $avatar = $host . 'point/img/header/' . $filename;
                $imgthumb = new helper_thumb ();
                $thumb = $imgthumb->getsquare ( $filepath, 180, 180 );
                $user->avatar = $avatar;
                 
            }
            $user->save();

            if(!empty($oldpath) && is_file($oldpath)){//新图片上传成功之后才删除旧图片 避免被占用过多的空间
                global $root_dir;
                $oldimgpath=$root_dir . '/' . 'public'. '/'.$oldpath;
                Helper_Common::safeDelFile($oldimgpath);
            }
             
            header('location: /baodan/index');die;
        }else{
            $this->_view['user'] = $user;
        }
    }
    
	function actionlogin(){  
	    if($this->_context->isPOST()){
	    
	        $account = $this->_context->account;
	        $password = $this->_context->password;
	        $acctuser = Acctuser::find('acctname = ?',$account)->getOne();
	         
	        if(!$acctuser->id()){
	            exit(json_encode(array('code'=>1,'msg'=>'用户未找到1')));
	        }
	        if($acctuser->password != $password){
	            exit(json_encode(array('code'=>2,'msg'=>'密码错误')));
	        }
	        $acctuser->is_login = 1;
	        $acctuser->last_logintime = date('Y-m-d H:i:s');
	        $acctuser->save();
	        $_SESSION['user'] = $acctuser->toArray();
	        setcookie("user_acctname",$account, time()+3600*24*12*30,'/');
	        setcookie("user_id",$acctuser->id, time()+3600*24*12*30,'/');
	        exit(json_encode(array('code'=>0,'msg'=>'ok')));
	    }
	}
	/**
	 * 个人中心
	 */
	function actionindex(){
	    
	    if(!isset($_COOKIE['user_id'])){
	        header('location: /baodan/login');die;
	       
	    }
	    $userid = $_COOKIE['user_id'];
	    $user = Acctuser::find('id = ?',$userid)->getOne();
	    if(!$user->id()){
	        message('用户未找到！','/baodan/login');
	    }else{
	        $this->_view['user'] = $user;
	    }
	    
	}
	/**
	 * 退出登录
	 */
	function actionloginout(){  
	    $user = Acctuser::find('id = ?',$_SESSION['user']['id'])->getOne();
	    $user->is_login == 0;
	    $user->save();
	    if(isset($_SESSION['user'])) unset($_SESSION['user']);
	    if(isset($_COOKIE['user_acctname'])){
	        setcookie("user_acctname",'',time() - 3600,'/');
	    } 
	    if(isset($_COOKIE['user_id'])){
	        setcookie("user_id",'',time() - 3600,'/');
	    }
	    header('location: /baodan/login');die;
	}
	/**
	 * 注册
	 */
	function actionregister(){

	    if($this->_context->isPOST()){
	        $bind_tel =   $this->_context->phone;
	        $password =  $this->_context->password;
	        $checkcode =   $this->_context->checkcode;
	        $realname =   $this->_context->realname;
	        $recacctname = $this->_context->recacctname;
	       // var_dump($recacctname);die; 
	        $agent_acctname = '';
	        $agent_realname = '';
	        $agenrteam_tag = '';
	        if($recacctname){
    	        $recuser = Acctuser::find('acctname = ?',$recacctname)->getOne();
    	       
    	        if($recuser->id()){
    	            $agent_acctname = $recuser->acctname;
    	            $agent_realname = $recuser->realname;
    	            $agenrteam_tag  = $recuser->team_tag;
    	        }	        
	        }
	     
	    
	        if(empty($realname)){
	            exit(json_encode(array('code'=>3,'msg'=>'真实姓名必须输入')));
	        }  
	        if(empty($password)){
	            exit(json_encode(array('code'=>3,'msg'=>'密码必须输入')));
	        }
	        if(empty($bind_tel)){
	            exit(json_encode(array('code'=>3,'msg'=>'手机号码必须输入')));
	        }
	    
	        $result =  Smsvoipauth::authCode($bind_tel,$checkcode);
	        if(isset($result['code']) && $result['code'] ==0){
	            $user = Acctuser::find('acctname = ?',$bind_tel)->getOne();
	            if(!$user->id()){//注册
	                 $data = array(
	                     'realname'=>$realname,
	                     'password'=>$password,
	                     'acctname'=>$bind_tel,
	                     'agent_acctname'=>$agent_acctname,
	                     'agent_realname'=>$agent_realname,
	                 );
	                 
	               $user = new Acctuser($data);
	               $user->save();
	               exit(json_encode(array('code'=>0,'msg'=>'注册成功','url'=>'/baodan/login')));
	            }else{
	               exit(json_encode(array('code'=>1,'msg'=>'该用户已经注册!')));
	            }
	        }else{
	            exit(json_encode(array('code'=>2,'msg'=>isset($result['msg'])?$result['msg']:'验证码校验出错')));
	        }
	    }else{
	        if(!empty($_GET['recacctname'])){
	          $recacctname = base64_decode($_GET['recacctname']);
	           
	        }else{
	            $recacctname = '';
	        }
	        $this->_view['recacctname'] = $recacctname;
	        
	    }
	    
	}
	/**
	 * 验证码
	 */
	function actiongetCatpcha(){
	    if($this->_context->isPOST()){
            $bind_tel =   $this->_context->phone;
            $op = $this->_context->op;
            if($op =='reg'){
                 $user = Acctuser::find('acctname = ?',$bind_tel)->getOne();
                 if($user->id()){//注册
                     exit(json_encode(array('code'=>3,'msg'=>'该用户已经注册')));
                 }
               
            }
            $mingday  = date("Y-m-d",strtotime("+1 day"));
            $today =   date("Y-m-d");
            $mingtime = strtotime($mingday);//昨天零点
            $todaytime = strtotime($today);//今天零点
            $mingtime = date('Y-m-d H:i:s',$mingtime);
            $todaytime = date('Y-m-d H:i:s',$todaytime);
            
            $count =Smsvoipauth::find('mobile = ? and create_time > ? and create_time < ?',$bind_tel,$todaytime,$mingtime)->getCount('id');
        //  echo $count;die;
            if($count > 6){
                exit(json_encode(array('code'=>1,'msg'=>'超过今日短信限制次数')));
            }
            $code = rand(1000,9999);
            $result = Smsvoipauth::sendAuthSms($bind_tel,$code);
          //  var_dump($result);die;
            if(isset($result['code']) && $result['code'] ==0){
                exit(json_encode(array('code'=>0,'msg'=>'短信发送成功，请注意查看')));
            }else{
                exit(json_encode(array('code'=>1,'msg'=>isset($result['msg'])?$result['msg']:'验证码校验出错')));
            }
	    }	    
	}
	/**
	 * 找回密码
	 */
	function actionforgetpwd(){
	
	    if($this->_context->isPOST()){
	        $bind_tel =   $this->_context->phone;
	        $password =  $this->_context->password;
	        $checkcode =   $this->_context->checkcode;
	        $realname =   $this->_context->realname;
	         
	        $result =  Smsvoipauth::authCode($bind_tel,$checkcode);
	        if(isset($result['code']) && $result['code'] ==0){
	            $user = Acctuser::find('acctname = ?',$bind_tel)->getOne();
	            if(!$user->id()){//     
	                exit(json_encode(array('code'=>1,'msg'=>'用户尚未注册!')));
	            }else{
	                $user->password = $password;
	                $user->save();
	                exit(json_encode(array('code'=>0,'msg'=>'操作成功','url'=>'/baodan/login')));
	            }
	        }else{
	            exit(json_encode(array('code'=>1,'msg'=>isset($result['msg'])?$result['msg']:'验证码校验出错')));
	        }
	    }
	}
	/**
	 * 申请成为报单中心
	 * 
	 */
	function actionapply(){
	     if($this->_context->isPOST()){
		    if(!isset($_COOKIE['user_id'])){
	          header('location: /baodan/login');die;
	        }else {
	            $user = Acctuser::find('id = ?',$_COOKIE['user_id'])->getOne();
	            if(!$user->id()){
	                header('location: /baodan/login');die;
	            }else{
	                $user->bd_status = -1;
	                $user->is_bd = 1;
	                $user->save();
	                 exit(json_encode(array('code'=>0,'msg'=>'申请成功，请等待管理人员联系和审核')));   
	            }
	        }
	     }
	}
	/**
	 * 平台信息
	 */
	
	function actioncontactus(){
	    
	}
	
	/**
	 * 外部跳转
	 */
	function actionapplogin(){
	    $username = $this->_context->mobile;//手机号码
	    $sign = $this->_context->sign;//秘钥
	    $signstr = md5($username.'huiboapp');
	    if($signstr == $sign){//这里写逻辑
	        echo 'ok';
	    }else{
	        echo '非法请求';
	    }
	}
}